package com.hs.srm.admin.modules.sys.shiro;

import org.apache.shiro.authc.HostAuthenticationToken;
import org.apache.shiro.authc.RememberMeAuthenticationToken;

/**
 * @program: HSSRM
 * @description: 自定义token
 * @author: Wang Pei Dong
 * @create: 2018-08-09 17:50
 **/
public class MyUsernamePasswordToken implements HostAuthenticationToken, RememberMeAuthenticationToken {

	private static final long serialVersionUID = -5573328326155961304L;
	private String token;
	private String loginType;
	private String username;
	private char[] password;
	private boolean rememberMe;
	private String host;

	public MyUsernamePasswordToken() {
		this.rememberMe = false;
	}

	public MyUsernamePasswordToken(String username, char[] password) {
		this(username, password, false, (String)null);
	}

	public MyUsernamePasswordToken(String username, char[] password, boolean rememberMe) {
		this(username, password, rememberMe, (String)null);
	}

	public MyUsernamePasswordToken(String username, char[] password, boolean rememberMe, String host) {
		this.rememberMe = false;
		this.username = username;
		this.password = password;
		this.rememberMe = rememberMe;
		this.host = host;
	}

	public MyUsernamePasswordToken(String username, char[] password, String host) {
		this(username, password, false, host);
	}

	public MyUsernamePasswordToken(String username, String password) {
		this(username, password != null ? password.toCharArray() : null, false, (String)null);
	}

	public MyUsernamePasswordToken(String username, String password, boolean rememberMe) {
		this(username, password != null ? password.toCharArray() : null, rememberMe, (String)null);
	}

	public MyUsernamePasswordToken(String username, String password, boolean rememberMe, String host) {
		this(username, password != null ? password.toCharArray() : null, rememberMe, host);
	}

	public MyUsernamePasswordToken(String username, String password, String host) {
		this(username, password != null ? password.toCharArray() : null, false, host);
	}

	public void clear() {
		this.username = null;
		this.host = null;
		this.rememberMe = false;
		if(this.password != null) {
			for(int i = 0; i < this.password.length; ++i) {
				this.password[i] = 0;
			}
			this.password = null;
		}
	}

	@Override
	public Object getCredentials() {
		return this.getPassword();
	}

	@Override
	public String getHost() {
		return this.host;
	}

	public String getLoginType() {
		return this.loginType;
	}

	public char[] getPassword() {
		return this.password;
	}

	@Override
	public Object getPrincipal() {
		return this.getUsername();
	}

	public String getToken() {
		return this.token;
	}

	public String getUsername() {
		return this.username;
	}

	@Override
	public boolean isRememberMe() {
		return this.rememberMe;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public void setLoginType(String loginType) {
		this.loginType = loginType;
	}

	public void setPassword(char[] password) {
		this.password = password;
	}

	public void setRememberMe(boolean rememberMe) {
		this.rememberMe = rememberMe;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append(this.getClass().getName());
		sb.append(" - ");
		sb.append(this.username);
		sb.append(", rememberMe=").append(this.rememberMe);
		if(this.host != null) {
			sb.append(" (").append(this.host).append(")");
		}
		return sb.toString();
	}
}
